
package com.bluelinelabs.conductor.conductor.changehandler;

import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

import com.bluelinelabs.conductor.conductor.ControllerChangeHandler;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class HorizontalChangeHandler extends AnimatorChangeHandler {

    public HorizontalChangeHandler() {
    }

    public HorizontalChangeHandler(boolean removesFromViewOnPush) {
        super(removesFromViewOnPush);
    }

    public HorizontalChangeHandler(long duration) {
        super(duration);
    }

    public HorizontalChangeHandler(long duration, boolean removesFromViewOnPush) {
        super(duration, removesFromViewOnPush);
    }

    @Override
    protected AnimatorGroup getAnimator(ComponentContainer container, Component from, Component to, boolean isPush, boolean toAddedToContainer) {

//        System.out.println("wtf HorizontalChangeHandler getAnimator  -from.getWidth() = " +  -from.getWidth() +
//                " to.getWidth() = " + to.getWidth() + " isPush = " + isPush);
        AnimatorGroup animatorGroup = new AnimatorGroup();
        AnimatorProperty fromAnimatorProperty = null;
        AnimatorProperty toAnimatorProperty = null;

        if (isPush) {
            if (from != null) {
                fromAnimatorProperty = from.createAnimatorProperty();
                fromAnimatorProperty.moveToX(-from.getWidth());
            }
            if (to != null) {
                toAnimatorProperty = to.createAnimatorProperty();
                toAnimatorProperty.moveFromX(to.getWidth()).moveToX(0);
            }
        } else {

            if (from != null) {
                fromAnimatorProperty = from.createAnimatorProperty();
                fromAnimatorProperty.moveFromX(0).moveToX(from.getWidth());
            }
            if (to != null) {
                // Allow this to have a nice transition when coming off an aborted push animation
                float fromLeft = from != null ? from.getTranslationX() : 0;
                toAnimatorProperty = to.createAnimatorProperty();
                toAnimatorProperty.moveFromX(fromLeft - to.getWidth()).moveToX(0);
            }
        }
        if (fromAnimatorProperty != null && toAnimatorProperty != null) {
            animatorGroup.runParallel(fromAnimatorProperty, toAnimatorProperty);
        } else if (fromAnimatorProperty != null) {
            animatorGroup.runParallel(fromAnimatorProperty);
        } else if (toAnimatorProperty != null) {
            animatorGroup.runParallel(toAnimatorProperty);
        }
        return animatorGroup;
    }

    @Override
    protected void resetFromView(Component from) {
        System.out.println("wtf HorizontalChangeHandler resetFromView");
        from.setTranslationX(0);
    }

    @Override
    public ControllerChangeHandler copy() {
        System.out.println("wtf ControllerChangeHandler copy ");
        return new HorizontalChangeHandler(getAnimationDuration(), removesFromViewOnPush());
    }
}
